//
//  Array+Extension.swift
//  TDDStudySwift
//
//  Created by baidu on 7/1/16.
//  Copyright © 2016 caoyuan. All rights reserved.
//

import Foundation

extension Array {
    //pop last object
    mutating func pyPop() -> Element? {
        return popLast()
    }

    //pop the object in index position, just like dequeue opteration in Queue
    mutating func pyPop(index:Int) -> Element? {
        if index >= 0 && index < self.count {
            let element = self[index]
            remove(at: index)
            return element
        }
        return nil
    }
    
    
    func forEach(block:(index:Index,value:Element)->Void) -> Void {
        for (index,value) in self.enumerated(){
            block(index: index, value: value)
        }
//        for i in 0..<self.count {
//            block(index: i, value: self[i])
//        }
    }
    
    mutating func pyInsert(index:Int, object:Element) -> Void {
        var i = index
        if i < 0 {
            i = 0
        }else if i > self.count-1 {
            i = self.count
        }
        self.insert(object, at: i)
    }
    
}


extension Array where Element : Equatable {
    //find object from array, if not have one return -1
    func pyFind(object:Element) -> Int {
        if let index = self.index(of: object){
            return index
        }else {
            return -1
        }
    }
    
}
